﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Threading;
using System.Globalization;
using UCenter.Core.Common;
using UCenter.DependenceInjection;
using UCenter.PluginFramework;

namespace UCenter.Web
{
    public class MvcApplication : HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = "" }
            );
        }

        IContainerContext repositoryContainer = ContainerManager.GetContainer("repositoryContainer");
        IContainerContext serviceContainer = ContainerManager.GetContainer("serviceContainer");
        
        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);

            repositoryContainer.Initialize();
            serviceContainer.Initialize();

            //PlugInManager.Setup();
        }

        protected void Application_BeginRequest()
        {
            CultureInfo ci = new CultureInfo(AppSettings.DefaultCulture);
            Thread.CurrentThread.CurrentCulture = ci;
            Thread.CurrentThread.CurrentUICulture = ci;
        }

        protected void Application_End()
        {
            repositoryContainer.TearDown();
            serviceContainer.TearDown();
        }
    }
}
